<?php
/**
 * @copyright SilverBiology, LLC
 * @author Balachandran Viswanathan
 */
Class Logger {
	public $db, $record;
	
	function __construct() {}

/**
 * Loads by logid
 * @param int $logId
 */
	public function load_by_logId($logId) {
		$query = sprintf("SELECT * FROM `log` WHERE `log_id` = '%s';", mysql_escape_string($logId) );
		$ret = $this->db->query_one( $query );
		if ($ret != NULL) {
			foreach( $ret as $field => $value ) {
				$this->record[$field] = $value;
			}
			return true;
		}
		return false;
	}

	public function load_by_occurrenceId($occurrenceId) {
		$query = sprintf("SELECT * FROM `log` WHERE `occurrence_id` = '%s';", mysql_escape_string($occurrenceId) );
		$ret = $this->_db->query_all( $query );
		return $ret;
	}

	public function load_by_imageId($imageId) {
		$query = sprintf("SELECT * FROM `log` WHERE `image_id` = '%s';", mysql_escape_string($imageId) );
		$ret = $this->_db->query_all( $query );
		return $ret;
	}

	public function save() {
		if($this->exists($this->get('log_id'))) {
			$query = sprintf("UPDATE `log` SET `occurrence_id` = '%s', `image_id` = '%s', `type` = '%s', `value` = '%s', `check_out` = '%s', `check_in` = '%s', `point_value` = '%s', `point_status` = '%s', `locked` = '%s', `expired` = '%s', `user_id` = '%s' WHERE `log_id` = '%s';"
			, mysql_escape_string($this->get('occurrence_id'))
			, mysql_escape_string($this->get('image_id'))
			, mysql_escape_string($this->get('type'))
			, mysql_escape_string($this->get('value'))
			, mysql_escape_string($this->get('check_out'))
			, mysql_escape_string($this->get('check_in'))
			, mysql_escape_string($this->get('point_value'))
			, mysql_escape_string($this->get('point_status'))
			, mysql_escape_string($this->get('locked'))
			, mysql_escape_string($this->get('expired'))
			, mysql_escape_string($this->get('user_id'))
			, mysql_escape_string($this->get('log_id'))
			);
		} else {
			$query = sprintf("INSERT INTO `log` SET `occurrence_id` = '%s', `image_id` = '%s', `type` = '%s', `value` = '%s', `check_out` = '%s', `check_in` = '%s', `point_value` = '%s', `point_status` = '%s', `locked` = '%s', `expired` = '%s', `user_id` = '%s';"
			, mysql_escape_string($this->get('occurrence_id'))
			, mysql_escape_string($this->get('image_id'))
			, mysql_escape_string($this->get('type'))
			, mysql_escape_string($this->get('value'))
			, mysql_escape_string($this->get('check_out'))
			, mysql_escape_string($this->get('check_in'))
			, mysql_escape_string($this->get('point_value'))
			, mysql_escape_string($this->get('point_status'))
			, mysql_escape_string($this->get('locked'))
			, mysql_escape_string($this->get('expired'))
			, mysql_escape_string($this->get('user_id'))
			);
		}
		if($this->db->query($query)) {
			return true;
		} else {
			return false;
		}
	}
	

/**
* Returns a since field value
* @return mixed
*/
	public function get( $field ) {
		if (isset($this->record[$field])) {
			return( $this->record[$field] );
		} else {
			return( false );
		}
	}

/**
* Set the value to a field
* @return bool
*/
	public function set( $field, $value ) {
		$this->record[$field] = $value;
		return( true );
	}

	public function exists ($logId){
		$query = sprintf("SELECT `log_id` FROM `log` WHERE `log_id` = '%s';", mysql_escape_string($logId) );
		$ret = $this->db->query_one( $query );
		if ($ret == NULL) {
			return false;
		} else {
			return true;
		}
	}
}
?>